package com.moli.iov.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.model.dto.req.PageConvert;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.sys.mapper.CompanyMapper;
import com.moli.iov.sys.model.dto.req.CompanyDto;
import com.moli.iov.sys.model.po.Company;
import com.moli.iov.sys.service.ICompanyService;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.StringUtil;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 企业表 服务实现类
 * </p>
 *
 * @author zql
 * @since 2019-09-04
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

  @Autowired
  private CompanyMapper companyMapper;

  /**
   *
   * 查询企业列表
   *
   * @param companyDto
   * @return
   */
  @Override
  public List<Company> listCompany(CompanyDto companyDto) {
    return companyMapper.selectList(assemblyConditions(companyDto));
  }

  /**
   *
   * 分页查询企业列表
   *
   * @param companyDto
   * @return
   */
  @Override
  public PageExtend<Company> listCompanyOfPage(CompanyDto companyDto){
    IPage<Company> companyIPage = companyMapper.selectPage(
        new Page<Company>(companyDto.getPageNum(), companyDto.getPageSize()),
        assemblyConditions(companyDto));
    return PageConvert.convert(companyIPage);
  }

  /**
   * 增加企业
   * @param companyDto
   * @return
   */
  @Override
  public Long saveCompany(CompanyDto companyDto) {
    Company company = new Company();
    CommonUtils.copyProperties(companyDto, company);
    companyMapper.insert(company);
    companyDto.setCompanyId(company.getCompanyId());
    return company.getCompanyId();
  }

  /**
   * 修改企业
   * @param companyDto
   * @return
   */
  @Override
  public int updateCompanyByCompanyId(CompanyDto companyDto) {
    Company company = new Company();
    CommonUtils.copyProperties(companyDto, company);
    QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("company_id", companyDto.getCompanyId());
    return companyMapper.update(company, queryWrapper);
  }

  /**
   * 获取单个企业
   * @param companyDto
   * @return
   */
  @Override
  public Company getCompanyByCompanyId(CompanyDto companyDto) {
    return companyMapper.selectById(companyDto.getCompanyId());
  }

  /**
   * 组装查询条件
   * @param companyDto
   * @return
   */
  private QueryWrapper<Company> assemblyConditions(CompanyDto companyDto){
    QueryWrapper<Company> queryWrapper = new QueryWrapper<>();

    if(companyDto == null){
      return queryWrapper;
    }

    if(companyDto.getCompanyId() != null){
      queryWrapper.eq("company_id", companyDto.getCompanyId());
    }

    if(companyDto.getNeCompanyId() != null){
      queryWrapper.ne("company_id", companyDto.getNeCompanyId());
    }

    if(companyDto.getCompanyIds() != null && !companyDto.getCompanyIds().isEmpty()){
      queryWrapper.in("company_id", companyDto.getCompanyIds());
    }

    if(StringUtil.isNotBlank(companyDto.getCompanyName())){
      queryWrapper.like("company_name", companyDto.getCompanyName());
    }else if(StringUtil.isNotBlank(companyDto.getAccurateCompanyName())){
      queryWrapper.eq("company_name", companyDto.getAccurateCompanyName());
    }

    if(StringUtil.isNotBlank(companyDto.getProvinceCode())){
      queryWrapper.eq("province_code", companyDto.getProvinceCode());
    }

    if(StringUtil.isNotBlank(companyDto.getCityCode())){
      queryWrapper.eq("city_code", companyDto.getCityCode());
    }

    if(StringUtil.isNotBlank(companyDto.getEmail())){
      queryWrapper.eq("email", companyDto.getEmail());
    }

    if(companyDto.getStatus() != null){
      queryWrapper.eq("status", companyDto.getStatus());
    }

    if(StringUtil.isNotBlank(companyDto.getCorporationPhone())){
      queryWrapper.eq("corporation_phone", companyDto.getCorporationPhone());
    }
    queryWrapper.orderByDesc("create_time");
    return queryWrapper;
  }
}
